//公共方法
(function(){

    window.commons = {  };

    //将时间戳转换为时间字符串
    commons.getTimeNumToStr = (_num, _format) => {
        Date.prototype.Format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份 
                "d+": this.getDate(), //日 
                "h+": this.getHours(), //小时 
                "m+": this.getMinutes(), //分 
                "s+": this.getSeconds(), //秒 
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
                "S": this.getMilliseconds() //毫秒 
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }
        return new Date(+_num).Format(_format);
    }

    //将时间补齐到yyyy-MM-dd hh:mm:ss
    commons.getFullTime = (_time, _top) => {
        if(!_time){
            return "";
        }

        let dateStr = _time.split(' ')[0],
            timeStr = _time.split(' ')[1] ? _time.split(' ')[1] : '00:00:00';

        let y = dateStr.split('-')[0] || '0000',
            M = dateStr.split('-')[1] || '00',
            d = dateStr.split('-')[2] || '00',
            h = _top ? '23' : (timeStr.split(':')[0] || '00'),
            m = _top ? '59' : (timeStr.split(':')[1] || '00'),
            s = _top ? '59' : (timeStr.split(':')[2] || '00');

        return y + '-' + M + '-' + d + ' ' + h + ':' + m + ':' + s;
    }

    //处理match以方便呈现到界面上
    commons.formatMatch = (match) => {
        //时间
        let date = commons.getTimeNumToStr(match.time, 'yy-MM-dd');
        //是否今天进行
        if(commons.today.indexOf(date) > -1){
            match.time = commons.getTimeNumToStr(match.time, '今天 hh:mm');
        }else{
            match.time = commons.getTimeNumToStr(match.time, 'MM月dd日 hh:mm');
        }
        //链接
        let defaultLink = "";
        let linkArr = [];
        if(match.links){
          let links = match.links.split(';');
          for(var i in links){
            let values = links[i].split(',');
            if(i==0 || values[0] == "default"){
              defaultLink = values[3]
            }
            if(values[0] != "default"){
              linkArr.push({
                'name': values[0],
                'title': values[1],
                'url': values[2]
              });
            }
          }
        }
        match.defaultLink = defaultLink;
        match.links = linkArr;
        return match;
    };

    //当前的日期
    let gettoday = () => {
        let now = new Date().getTime();
        let today = commons.getTimeNumToStr(now, 'yyyy-MM-dd');
        return commons.getFullTime(today);
    };
    commons.today = gettoday();

    //当前客户端类型
    let judgeClient = () => {
        debugger;
        let userAgent = navigator.userAgent.toLowerCase();
        let mobiles = [
            "mobile",
            "android", 
            "iphone",
            "symbianos", 
            "windows phone",
            "ipad", 
            "ipod"
        ];
        let ispc = true;
        for(var v of mobiles){
            if(userAgent.indexOf(v) > 0){
                ispc = false;
                break;
            }
        }
        return ispc;
    };
    commons.ispc = judgeClient();

})();